<?php
class Order_profit_model extends CI_Model
{
    private $table = 'order_profit';
    private $table_2 = 'order_base';
    
    
     /**
     * 根据账户类型和订单iD来获取结果
     * @param unknown $order_id
     * @param unknown $bit_trade
     */
    public function getResultByOrderIdAndBitTrade($order_id,$bit_trade)
    {
    	$this->db->where('order_id',$order_id);
    	$this->db->where('bit_trade',$bit_trade);
    	return $this->db->get($this->table);
    }
    
    public function findById($order_id)
    {
        $this->db->where('order_id', $order_id);
        return $this->db->get($this->table);
    }

    public function addBatch($data)
    {
        return $this->db->insert_batch($this->table, $data);
    }
    
    public function delete($order_id)
    {
        $this->db->where('order_id', $order_id);
        return $this->db->delete($this->table);
    }
    
    public function insertBatch($params = array())
    {
        foreach ($params as $orderId=>$profit) {
            if (!empty($profit)) {
                foreach ($profit as $k=>$v) {
                    $data[] = array(
                        'order_id'  => $orderId,
                        'us'        => $v['us'],
                        'ut'        => $v['ut'],
                        'as'        => $v['as'],
                        'at'        => $v['at'],
                        'amount'    => $v['amount'],
                        'step'      => $v['step'],
                        'bit_trade' => $v['bit_trade']
                    );
                }
            }
        }
        if (!empty($data)) {
            return $this->db->insert_batch($this->table, $data);
        }
        return true;
    }
    
     /**
     * @descripte   分润
     * @Author xiumao
     * @date 2016/6/28 0028 下午 3:41
     */
    public function profit_total($params = array())
    {
        $this->db->join($this->table_2 . ' as order_base', 'order_base.order_id = order_profit.order_id');

        if ( !empty($params['uid']) ) {
            $this->db->where('order_profit.ut', $params['uid']);
        }
        if ( !empty($params['payer_uid']) ) {
            $this->db->where('order_base.payer_uid', $params['payer_uid']);
        }
        if ( !empty($params['order_id']) ) {
            $this->db->where('order_profit.order_id', $params['order_id']);
        }
        if ( !empty($params['startTime']) ) {
            $this->db->where('order_base.ordertime >=', strtotime($params['startTime'] . ' 00:00:00'));
        }
        if ( !empty($params['endTime']) ) {
            $this->db->where('order_base.ordertime <=', strtotime($params['endTime'] . ' 23:59:59'));
        }
        if ( !empty($params['status']) && $params['status'] == 2 ) {
            $this->db->where("order_base.bit_use >", 0);
        } else if ( !empty($params['status']) && $params['status'] == 1 ) {
            $this->db->where("order_base.bit_use", 0);
        }
        $this->db->where('(order_base.bit_pay & 65536) =', 65536);
        $this->db->where('(order_base.bit_opt & 128) =', 0);//未退票
        $this->db->where('order_base.number >', 0);//未退票
        $this->db->where_in('order_base.cat_id', array(1, 2, 4, 8, 16));

        return $this->db->count_all_results($this->table);
    }

    /**
     * @descripte   分润
     * @Author xiumao
     * @date 2016/6/28 0028 下午 3:55
     */
    public function profit_page_list($page_num, $num, $params = array())
    {
        $this->db->select('order_base.price,
                        order_base.cat_id,
                        order_base.number,
                        order_base.bit_use,
                        order_base.goods_name,
                        order_base.ordertime,
                        order_base.payer_name,
                        order_base.amount_total,
                        order_profit.amount');
        $this->db->join($this->table_2 . ' as order_base', 'order_base.order_id = order_profit.order_id');

        if ( !empty($params['uid']) ) {
            $this->db->where('order_profit.ut', $params['uid']);
        }
        if ( !empty($params['payer_uid']) ) {
            $this->db->where('order_base.payer_uid', $params['payer_uid']);
        }
        if ( !empty($params['order_id']) ) {
            $this->db->where('order_profit.order_id', $params['order_id']);
        }
        if ( !empty($params['startTime']) ) {
            $this->db->where('order_base.ordertime >=', strtotime($params['startTime'] . ' 00:00:00'));
        }
        if ( !empty($params['endTime']) ) {
            $this->db->where('order_base.ordertime <=', strtotime($params['endTime'] . ' 23:59:59'));
        }
        if ( !empty($params['status']) && $params['status'] == 2 ) {
            $this->db->where("order_base.bit_use >", 0);
        } else if ( !empty($params['status']) && $params['status'] == 1 ) {
            $this->db->where("order_base.bit_use", 0);
        }
        $this->db->where('(order_base.bit_pay & 65536) =', 65536);
        $this->db->where('(order_base.bit_opt & 128) =', 0);//未退票
        $this->db->where('order_base.number >', 0);//未退票
        $this->db->where_in('order_base.cat_id', array(1, 2, 4, 8, 16));
        $this->db->from($this->table);
        $this->db->order_by('order_base.ordertime', 'DESC');
        $this->db->limit($page_num, $num);

        return $this->db->get();
    }

    /**
     * @descripte   分润已到账收入
     * @Author xiumao
     * @date 2016/6/28 0028 下午 4:55
     */
    public function profit_account($params = array())
    {
        $this->db->select_sum('amount');
        $this->db->join($this->table_2 . ' as order_base', 'order_base.order_id = order_profit.order_id');

        if ( !empty($params['uid']) ) {
            $this->db->where('order_profit.ut', $params['uid']);
        }
        if ( !empty($params['payer_uid']) ) {
            $this->db->where('order_base.payer_uid', $params['payer_uid']);
        }
        if ( !empty($params['order_id']) ) {
            $this->db->where('order_profit.order_id', $params['order_id']);
        }
        if ( !empty($params['startTime']) ) {
            $this->db->where('order_base.ordertime >=', strtotime($params['startTime'] . ' 00:00:00'));
        }
        if ( !empty($params['endTime']) ) {
            $this->db->where('order_base.ordertime <=', strtotime($params['endTime'] . ' 23:59:59'));
        }
        if ( !empty($params['status']) && $params['status'] == 2 ) {
            $this->db->where("order_base.bit_use >", 0);
        } else if ( !empty($params['status']) && $params['status'] == 1 ) {
            $this->db->where("order_base.bit_use", 0);
        }
        $this->db->where('(order_base.bit_pay & 65536) =', 65536);
        $this->db->where('(order_base.bit_opt & 128) =', 0);//未退票
        $this->db->where('order_base.number >', 0);//未退票
        $this->db->where_in('order_base.cat_id', array(1, 2, 4, 8, 16));
        $this->db->from($this->table);

        return $this->db->get();
    }
    
    
}